import nci60.data.connected.LeadingProtein
import nci60.data.maxquant.Project

//specify the directory in which the supplementary files will be created
def outputPath = 'some/directory/'

println 'start'
//projects[ProjectId][ProjectName]
def projects = [:]
//file[ProjectId][File]
def files = [all:(new File(outputPath+'proteins_all.csv'))]
//celllines[ProjectId][CelllineId][CelllineName]
def celllines = [:]
Project.findAll().each{proj->
    projects.put(proj.id, proj.name)
    files.put(proj.id, new File(outputPath+'proteins_'+proj.name+'.csv'))
    celllines.put(proj.id, [:])
}

println 'projects done'

LeadingProtein.executeQuery('select c.currentProject.id, c.id, c.name from Experiment c order by c.name').each{line->
    def proj = line[0]
    def cel = line[1]
    def celName = line[2]
    celllines[proj].put(cel, celName)
}

println 'experiments done'
//data[projectId][ProteinId][CelllineId]=Intensity
def data = [:]
//proteins[ProteinId][ProjectId]=true
def proteins = [:]

LeadingProtein.executeQuery('select e.protein, e.currentProject.id, e.lfqIntensity, e.experiment.id from ProteinGroupExperiment e where e.protein!=0').each{line->
    def protId = line[0].toLong() 
    def curProject = line[1]
    def intensity = (line[2]!=0)?Math.log10(line[2]):0.0
    def cel = line[3]
    if(!data.containsKey(curProject)){data.put(curProject,[:])}
    if(!data[curProject].containsKey(protId)){data[curProject].put(protId,[:])}
    data[curProject][protId].put(cel, intensity)

    if(!proteins.containsKey(protId)){proteins.put(protId, [:])}
    proteins[protId].put(curProject, true)
}

println 'intensities done'

//genes[ProteinId][GeneNames]
def genes = [:]
LeadingProtein.executeQuery('select g.protein.id, g.name from GeneName g where g.protein.id in :prots', [prots:(proteins.keySet() as List)]).each{line->
    def prot = line[0]
    def gene = line[1]
    if(!genes.containsKey(prot)){genes.put(prot, [])}
    genes[prot].add(gene)
}

println 'genes done'

def proteinInfo = [:]
LeadingProtein.executeQuery('select p.id, p.mainId from Protein p where p.id in :prots', [prots:(proteins.keySet() as List)]).each{line->
    def prot = line[0]
    def mainId = line[1]
    proteinInfo.put(prot, mainId)
}

println 'protein info done'


//data[projectId][ProteinId][CelllineId]=Intensity
data.each{proj,projData->
    def headline = 'Protein\tGenes'
    celllines[proj].each{cel,celName->
        headline = headline+'\t'+celName
    }
    files[proj] << headline+'\n'
    projData.sort{it.key}.each{prot,protData->
        def outLine = ''+proteinInfo[prot]+'\t'+mergeList(genes[prot], ',')
        celllines[proj].each{cel,celName->
            if(protData.containsKey(cel)){
                outLine = outLine+'\t'+protData[cel]
            }else{
                outLine = outLine+'\t0'
            }
        }
        files[proj] << outLine+'\n'
    }
}

def headline = 'Protein\tGenes'
projects.each{proj,projName->
    headline = headline+'\t'+projName
}
files.all << headline+'\n'
proteins.sort{it.key}.each{prot,protData->
    def outLine = ''+proteinInfo[prot]+'\t'+mergeList(genes[prot], ',')
    projects.each{proj,projName->
        if(protData.containsKey(proj)){
            outLine = outLine+'\ttrue'
        }else{
            outLine = outLine+'\tfalse'
        }
    }
    files.all << outLine+'\n'
}

println 'done!!'
println 'Nr of Proteins: '+proteins.size()